package com.aqgj.UserRole.service;

import com.aqgj.Role.bean.Role;
import com.aqgj.Role.dao.RoleDao;
import com.aqgj.UserRole.bean.UserRole;
import com.aqgj.UserRole.dao.UserRoleDao;
import com.aqgj.util.StringUtils;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


@Service("UserRole")
public class UserRoleManager {
    @Autowired
    UserRoleDao userRoleDao;
    @Autowired
    RoleDao roleDao;

    public Map<String, Set<String>> selectResourceMapByUserId(String userId) {
        Map<String, Set<String>> resourceMap = new HashMap<String, Set<String>>();
        List<String> roleIdList = userRoleDao.selectRoleIdListByUserId(userId);
        Set<String> urlSet = new HashSet<String>();
        Set<String> roles = new HashSet<String>();
        for (String roleId : roleIdList) {
            List<Map> resourceList = roleDao.selectResourceListByRoleId(roleId);
            if (resourceList != null) {
                for (Map<String,String> map : resourceList) {
                    if (StringUtils.isNotBlank(map.get("url"))) {
                        urlSet.add(map.get("url"));
                    }
                }
            }
            Role role = roleDao.get(roleId);
            if (role != null) {
                roles.add(role.getName());
            }
        }
        resourceMap.put("urls", urlSet);
        resourceMap.put("roles", roles);
        return resourceMap;
    }

    public void save(UserRole userRole) {
       userRoleDao.save(userRole);
    }
}
